package org.ucas.faker.web.dao.patents;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.ucas.faker.web.entity.patents.PatentMeta;

import java.util.List;

@Repository
public interface MetaDao extends JpaRepository<PatentMeta, Long> {

  @Query("SELECT meta.id FROM PatentMeta meta WHERE meta.id in " +
      "( SELECT r.patentId FROM Record r WHERE r.depotId = ?1) " )
  public List<Long> toGenIds(long flowId);

  @Modifying
  @Query("UPDATE PatentMeta meta SET meta.parsed = true WHERE meta.id = :id")
  public void setGenerated(@Param(value = "id") long id);

  @Query("SELECT COUNT(*) FROM PatentMeta m WHERE m.id = ?1")
  public boolean exists(long id);

  @Query("SELECT meta FROM PatentMeta meta WHERE meta.source = ?1 AND meta.sourceId = ?2")
  public PatentMeta load(PatentMeta.Source source, String sourceId);

  @Query("SELECT COUNT(*) FROM PatentMeta meta WHERE meta.source = ?1 AND meta.sourceId = ?2")
  public boolean exists(PatentMeta.Source source, String sourceId);
}
